package com.maker.generator.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.dwork.base.controller.BaseController;
import com.dwork.base.controller.entity.BaseResultObject;
import com.dwork.base.controller.utils.JsonUtil;
import com.dwork.base.validate.groups.DEFAULT;
import com.maker.generator.entity.MetaGenDBInfo;
import com.maker.generator.entity.MetaGenObj;
import com.maker.generator.entity.MetaGenTable;
import com.maker.generator.event.GenerateEvent;
import com.maker.generator.service.IGenService;
import com.maker.generator.utils.TransEntityUtil;
@Controller
@RequestMapping(value = "gen", name = "代码工坊模块")
public class GeneratorController extends BaseController{
	private static Logger logger = LoggerFactory.getLogger(GeneratorController.class);
	@Autowired  
	private ApplicationContext ctx = null;
	@Autowired
	private IGenService genService;
	
	@RequestMapping(value = "/g2manage", name = "进入生成管理界面")
	public ModelAndView manage() {
		ModelAndView mv = getBaseMV("/gen/manage");
		return mv;
	}
	
	
	
	@RequestMapping(value = "/g2ojbguide", name = "基于元对象的生成器")
	@ResponseBody
	public ModelAndView g2ojbguide() {
		ModelAndView mv = getBaseMV("/gen/objguide");
		return mv;
	}
	
	@RequestMapping(value = "/g2preViewEntity", name = "进入代码预查看页面")
	@ResponseBody
	public ModelAndView g2preViewEntity(Long objId,Long pConfigId,Long mConfigId,Long oConfigId,Long aConfigId) {
		ModelAndView mv = getBaseMV("/gen/preViewEntity");
		MetaGenObj entity = this.genService.findMetaGenObjById(objId);
		mv.addObject("entityCode",TransEntityUtil.generateEntityString(entity,null));
		return mv;
	}
	@RequestMapping(value = "/g2preViewTable", name = "进入元表预查看页面")
	@ResponseBody
	public ModelAndView g2preViewTable(Long objId,Long pConfigId,Long mConfigId,Long oConfigId,Long aConfigId) {
		ModelAndView mv = getBaseMV("/gen/preViewTable");
		MetaGenObj entity = this.genService.findMetaGenObjById(objId);
		MetaGenTable table =TransEntityUtil.generateTableInfo(entity,null); 
		mv.addObject("table",table);
		mv.addObject("fields",JsonUtil.toJson(table.getGenFields()));
		
		return mv;
	}
	@RequestMapping(value = "/g2preViewMapper", name = "进入mapper预查看页面")
	@ResponseBody
	public ModelAndView g2preViewMapper(Long objId,Long pConfigId,Long mConfigId,Long oConfigId,Long aConfigId) {
		ModelAndView mv = getBaseMV("/gen/preViewMapper");
		MetaGenObj entity = this.genService.findMetaGenObjById(objId);
		MetaGenDBInfo dbInfo = new MetaGenDBInfo();
		dbInfo.setDbType(MetaGenDBInfo.DBTYPE_ORACLE);
		mv.addObject("mapperXML",TransEntityUtil.generateMapperString(entity,dbInfo,null));
		return mv;
	}
	@RequestMapping(value = "/g2preViewDAO", name = "进入DAO预查看页面")
	@ResponseBody
	public ModelAndView g2preViewDAO(Long objId,Long pConfigId,Long mConfigId,Long oConfigId,Long aConfigId) {
		ModelAndView mv = getBaseMV("/gen/preViewDAO");
		MetaGenObj entity = this.genService.findMetaGenObjById(objId);
		mv.addObject("daoCode",TransEntityUtil.generateDAOString(entity,null));
		return mv;
	}
	
	@RequestMapping(value = "/g2preViewIService", name = "进入IService预查看页面")
	@ResponseBody
	public ModelAndView g2preViewIService(Long objId,Long pConfigId,Long mConfigId,Long oConfigId,Long aConfigId) {
		ModelAndView mv = getBaseMV("/gen/preViewIService");
		MetaGenObj entity = this.genService.findMetaGenObjById(objId);
		mv.addObject("iServiceCode",TransEntityUtil.generateIServiceString(entity,null));
		return mv;
	}
	@RequestMapping(value = "/g2preViewServiceImpl", name = "进入ServiceImpl预查看页面")
	@ResponseBody
	public ModelAndView g2preViewServiceImpl(Long objId,Long pConfigId,Long mConfigId,Long oConfigId,Long aConfigId) {
		ModelAndView mv = getBaseMV("/gen/preViewServiceImpl");
		MetaGenObj entity = this.genService.findMetaGenObjById(objId);
		mv.addObject("serviceImplCode",TransEntityUtil.generateServiceImplString(entity,null));
		return mv;
	}
	@RequestMapping(value = "/g2preViewController", name = "进入Controller预查看页面")
	@ResponseBody
	public ModelAndView g2preViewController(Long objId,Long pConfigId,Long mConfigId,Long oConfigId,Long aConfigId) {
		ModelAndView mv = getBaseMV("/gen/preViewController");
		MetaGenObj entity = this.genService.findMetaGenObjById(objId);
		mv.addObject("controllerCode",TransEntityUtil.generateControllerString(entity,null));
		return mv;
	}
	
	
	
	
	
	
	/**
	 * 生成事件发布
	 */
	public void GenerateEvent(){
		String templateDir = "";
		String templateName ="";
		String rootOutDir = this.getRequest().getSession().getServletContext().getRealPath("/");		
		GenerateEvent event = new GenerateEvent(GenerateEvent.GENERATE_TYPE_DAO, "1","1","1",templateDir,templateName,".dao",rootOutDir);
		if(logger.isInfoEnabled()){
			logger.info("发布生成事件:");
		}
		this.ctx.publishEvent(event);
		
	}

}
